
package cn.edu.scau.cmi.chenzehan.hibernateframe.DAO;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.chenzehan.hibernateframe.POJO.HibernateUtil;
import cn.edu.scau.cmi.chenzehan.hibernateframe.POJO.Student;

public class StudentDAO {

	//保存学生信息
	public void save(Student student) {
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.save(student);
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
		finally {
			HibernateUtil.closeSession();
		}
	}
	//根据id查找指定id
	public Student findById(int studentID) {
		Student student = null;
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		try {
			student = (Student) session.get(Student.class, studentID);
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
		finally {
			HibernateUtil.closeSession();
		}
		return student;
	}
	
	//删除用户
	public void delete(Student student) {
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.delete(student);
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
		finally {
			HibernateUtil.closeSession();
		}
	}
	
	//修改用户信息
	public void update(Student student) {
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.update(student);
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
		finally {
			HibernateUtil.closeSession();
		}
	}
}
